package com.bwt.stack;

public class Calculator {
    public static void main(String[] args) {
        String expression = "300+200*616-200";
        // 创建两个栈 数栈 和符号栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);

        //定义需要的相关变量
        int index = 0;// 用于扫描的
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' '; // 将每次扫描到的char保存到ch中
        String keepNum = ""; //用于拼接多位数

        while (true) {
            //依次得到expression 的每一个字符
            ch = expression.charAt(index);
            //判断ch是什么就做相应的处理
            if (operStack.isOper(ch)) {
                //判断当前的符号栈 是否为空
                if (!operStack.isEmpty()) {
                    //如果符号栈有操作符, 就进行比较, 如果当前的操作符小于或等于栈中的栈顶的操作符,就需要从数栈中pop中两个数
                    //在符号栈中pop出一个符号, 运算后 结果存入数栈, 再将当前的符号压入符号栈
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                } else {
                    //为空直接入栈
                    operStack.push(ch);
                }
                index++;
            } else {
                //如果是数, 则直接入数栈
                while (!operStack.isOper(ch)) {
                    keepNum += ch;
                    index++;
                    if (index >= expression.length()) {
                        break;
                    }
                    ch = expression.charAt(index);

                }
                numStack.push(Integer.parseInt(keepNum));
                keepNum = "";
            }

            // 让index +1 , 并判断是否扫描到expression 的最后
            if (index >= expression.length()) {
                break;
            }
        }

        //
        while (true) {
            // 如果符号栈为空, 则计算到最后的结果, 数栈只有一个数字 [结果]
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }

        System.out.println("表达式: " + expression + " || 结果为: " + numStack.pop());
    }
}

//先创建一个栈
//定义ArrayStack 表示栈
class ArrayStack2 {
    private int maxSize; // 栈的大小
    private int[] stack;
    private int top = -1; // 表示栈顶 ,初始为-1

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    // 将栈顶的数据返回
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int i = stack[top];
        top--;
        return i;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈空, 没有数据");
        }
        for (int i = top; i >= 0; i--) {
            System.out.println("stack[" + i + "]" + "=" + stack[i]);
        }
    }

    //返回运算符的优先级, 优先级是程序员来确定的,  优先级使用数字表示
    // 数字越大, 则优先级越高
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1; //假定目前的表达式 只有 + - * /
        }
    }

    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    public int cal(int num1, int num2, int oper) {
        // res 用于存放计算的结果
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
        }
        return res;
    }

    //返回当前栈顶的值, 但是不是真正的出栈
    public int peek() {
        return stack[top];
    }
}
